let index;

function getUrlParam(paramName) {
    const urlParams = new URLSearchParams(window.location.search);
    return urlParams.get(paramName);
}

$(document).ready(function(){
    if(getUrlParam("s_url")!=""){
        $("#tempcookie").val(getUrlParam("cookie"));
        $("#url").val(getUrlParam("s_url"));
        $("#reset").prop("checked",true);
        $("#url").trigger("change");
    }
})
$(document).on("click","#geturl",function(){
    index = layer.load(0, {shade: false});
    clear();
    $("#json").val("");
    $("#filter").val("");
    if($("#url").val()!="") {
        $.ajax({
            type: 'post',
            async: true,//true是异步，false是同步
            timeout: 20000,//20秒，这里是毫秒
            url: "json.php?geturl=1",
            data: {
                "url": $("#url").val(),
                "method": $("#method").val(),
                "element": $("#element").val(),
                "data": $("#postdata").val(),
                "header": $("#header").val(),
                "cookie": $("#cookie").val(),
                "reset": $("#reset").val(),
                "gbk": $("#gbk").val(),
                "tojsdata": $("#tojsdata").val(),
            },
            dataType: "json",//html,text,json,excel,script
            success: function (data) {
                layer.close(index);
                $("#host").val(data.host);
                $("#id").val(data.id);
                $("#json").val(data.json);
                $("#name").val(data.name);
                $("#likere").val(data.likere);
                $("#filters").val(data.filters);
                $("#data2").val(data.data2);
                $("#selecturls").html(data.urlsnames);
                $("#data,#data5").val(data.data);
                $(".data3,#data3").val(data.data3);
                $("#data4").val(data.data4);
                $("#data7").val(data.data7);
                $("#finally_prefix").val(data.finally_prefix);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                ajaxTimeoutTest.abort();
                layer.close(index);
                layer.msg("超时了，请重试！");

            },
            complete : function(XMLHttpRequest,status){
                if(status=='timeout'){
                    ajaxTimeoutTest.abort();
                    layer.close(index);
                    layer.msg("超时了，请重试！");
                }
            }
        })
    }
})
$(document).on("change","#url",function(){
    clear()
    if($("#url").val()!="") {
        $.ajax({
            type: 'get',
            async: true,//true是异步，false是同步
            timeout: 20000,//20秒，这里是毫秒
            url: "./json.php?selecturl=1",
            data: {
                "url": $("#url").val(),
            },
            dataType: "json",//html,text,json,excel,script
            success: function (data) {
                layer.close(index);
                $("#host").val(data.host);
                $("#id").val(data.id);
                $("#name").val(data.name);
                $("#postdata").val(data.postdata);
                $("#cookie").val($("#tempcookie").val()!=""?$("#tempcookie").val():data.cookie);
                $("#header").val(data.header);
                $("#likere").val(data.likere);
                $("#filter").val(data.filter);
                $("#method").val(data.method);
                $("#element").val(data.element);
                $("#json").val(data.json);
                $("#gbk").prop("checked",data.gbk==1?true:false);
                $("#reset").prop("checked",true);
                $("#selecturls").html(data.urlsnames);
                $("#data").val(data.data);
                $("#data2").val(data.data2);
                $("#data3").val(data.data3);
                $("#data4").val(data.data4);
                $("#data5").val(data.data);
                $("#finally_prefix").val(data.finally_prefix);
                $("#tojsdata").val(data.tojsdata);
                $("#geturl").trigger("click");
            }
        });
    }
})
$(document).on("click","#save",function(){
    var urls= [
        "/collect/reset/json.php?save=1",
        "http://collect.longsunhd.com/collect/reset/json.php?save=1",
        //"http://collect3.longsunhd.com/collect/reset/json.php?save=1",
        //"http://collect34.longsunhd.com/collect/reset/json.php?save=1",
    ];
    for(var i=0;i<urls.length;i++)
    {
        $.ajax({
            type: 'post',
            async: true,//true是异步，false是同步
            timeout: 20000,//20秒，这里是毫秒
            url: urls[i],
            data: {
                "url": $("#url").val(),
                "host": $("#host").val(),
                "id": $("#id").val(),
                "name": $("#name").val(),
                "postdata": $("#postdata").val(),
                "json": $("#json").val(),
                "element": $("#element").val(),
                "cookie": $("#tempcookie").val()!=""?$("#tempcookie").val():$("#cookie").val(),
                "header": $("#header").val(),
                "likere": $("#likere").val(),
                "filter": $("#filter").val(),
                "method": $("#method").val(),
                "data": $("#data").val(),
                "data2": $("#data2").val(),
                "data3": $("#data3").val(),
                "data4": $("#data4").val(),
                "tojsdata": $("#tojsdata").val(),
                "finally_prefix": $("#finally_prefix").val(),
                "gbk": $("#gbk").val(),
            },
            dataType: "json",//html,text,json,excel,script
            success: function (data) {
                if ($("#likere").val() == "") $("#likere").val(data.data.likere)
                if ($("#host").val() == "") $("#host").val(data.data.host)
                if ($("#id").val() == "") $("#id").val(data.data.id)
                layer.msg(data.mes, {time: 15000})
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                ajaxTimeoutTest.abort();
                layer.close(index);
                layer.msg("超时了，请重试！");

            },
            complete : function(XMLHttpRequest,status){
                if(status=='timeout'){
                    ajaxTimeoutTest.abort();
                    layer.close(index);
                    layer.msg("超时了，请重试！");
                }
            }
        });
    }
})


//粘贴
document.addEventListener("paste", function (e) {
    debugger;
    if($(':focus').length>0) return;
    var url = e.clipboardData.getData("text");
    if(url.includes('###')){
        const [url, cookie] = clipboardText.split("###");
        $("#tempcookie").val(cookie);
    }
    if (/^http/.test(url)){
        $("#url").val(url);
        $("#url").trigger("change");
        $("#reset").prop("checked",true);
    }else if (/^(post|get)\s+(.*)\s+http[^\r\n]*([\s\S]+?)(?:(?:\r?\n){2,}([\s\S]+))?$/ig.test(url.trim())){
        var nn=/^(post|get)\s+(.*)\s+http[^\r\n]*([\s\S]+?)(?:(?:\r?\n){2,}([\s\S]+))?$/ig.exec(url.trim())
        nn[3]=nn[3].replace(/Cookie\:[^\r\n]+/i,"").trim()
        // if(/\{.*\:.*\}/gi.test(nn[4])){
        //     nn[3] = nn[3].replace(/\nContent\-Type\:[^\r\n]+/i, "") + '\nContent-Type: application/json'
        // }else {
        //     nn[3]=nn[3].replace(/\nContent\-Type\:[^\r\n]+/i,"")+'\nContent-Type: application/x-www-form-urlencoded'
        // }
        $("#method").val(nn[1].trim());
        $("#url").val(nn[2].trim());
        $("#header").val(nn[3].replace(/[\r\n]{2,}/g,"\n"));
        if(nn[4]) {
            $("#postdata").val(nn[4].trim());
        }else{
            $("#postdata").val("");
        }
        var cnn=/Cookie\:([^\r\n]+)/i.exec(url);
        if(cnn!=null){
            $("#cookie").val(cnn[1].trim());
            $.ajax({
                type: 'post',
                async: true,//true是异步，false是同步
                timeout: 200000,//20秒，这里是毫秒
                url: "./json.php?savecookie=1",
                data: {
                    "cookie": cnn[1].trim(),
                    "url": $("#url").val()
                },
                dataType: "json",
                success: function (data) {
                }
            });
        }else{
            $("#cookie").val("");
        }
        $("#reset").prop("checked",true);
        $("#geturl").trigger("click");
    }
});

//选择跳转
$(document).on("change","#selecturls",function() {
    $("#url").val($("#selecturls>option:selected").attr("value"));
    $("#url").trigger("change");
})

function help(o){
    layer.open({
        type: 2,
        title: '帮助',
        shadeClose: true,
        area: ['1000px', '380px'],
        skin: 'layeroverflowhidden',
        scrollbar: false,
        content: ['/collect/reset/help.php?o='+o,'no'] //iframe的url
    });
}

$(document).ready(function() {
    $("#data").scroll(function () {
        $("#data2").prop("scrollTop",$("#data").prop("scrollTop"));
    });
    $("#data2").scroll(function () {
        $("#data").prop("scrollTop",$("#data2").prop("scrollTop"));
    });
});

$(document).on("click","#guolv",function () {
    $("#jsonscript").remove();
    const script = document.createElement('script');
    script.id = 'jsonscript';
    script.type = 'text/javascript';
    script.language = 'javascript';
    script.src = '../js/json.js?v=0.1';
    document.head.appendChild(script);
    index = layer.load(0, {shade: false})

    clear()
    let ajaxTimeoutTest=$.ajax({
        type: 'post',
        async: true,//true是异步，false是同步
        timeout: 200000,//20秒，这里是毫秒
        url: "./json.php?dblclick=1&XDEBUG_SESSION_START=5435",
        data: {
            "json": $("#json").val(),
            "filters": $("#filters").val(),
            "url":$("#url").val(),
            "header": $("#header").val(),
            "postdata": $("#postdata").val(),
        },
        dataType: "json",//html,text,json,excel,script
        success: function (data) {
            layer.close(index)
            $("#data").val(data.data);
            $("#data2").val(data.data2);
            $("#data4").val(data.data4);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            ajaxTimeoutTest.abort();
            layer.close(index);
            layer.msg("超时了，请重试！");

        },
        complete : function(XMLHttpRequest,status){
            if(status=='timeout'){
                ajaxTimeoutTest.abort();
                layer.close(index);
                layer.msg("超时了，请重试！");
            }
        }
    });
});

$(document).on("click","#getcookie",function () {
    index = layer.load(0, {shade: false});
    clear()
    $.ajax({
        type: 'post',
        async: true,//true是异步，false是同步
        timeout: 20000,//20秒，这里是毫秒
        url: "json.php?getcookie=1",
        data: {
            "url": $("#url").val(),
        },
        dataType: "json",//html,text,json,excel,script
        success: function (data) {
            layer.close(index)
            $("#cookie").val(data.cookie);
        }
    });
});

$(document).on("click","#testloginin",function () {
    index = layer.load(0, {shade: false});
    top.location="testloginin://"
});


$(document).on("click","#savecookie",function () {
    index = layer.load(0, {shade: false});
    clear()
    $.ajax({
        type: 'post',
        async: true,//true是异步，false是同步
        timeout: 20000,//20秒，这里是毫秒
        url: "json.php?savecookie=1",
        data: {
            "url": $("#url").val(),
            "cookie":$("#cookie").val(),
        },
        dataType: "json",//html,text,json,excel,script
        success: function (data) {
            layer.close(index)
            $("#cookie").val(data.cookie);
        }
    });
});

$(document).on("blur","#filter",function(){
    var nn=/^\s*([^\:\r\n]+)(\[\"([^\r\n\:]+)\"\])(\[[0-9]+\]+)([^\:\r\n]*)(\[\"([^\r\n\:]+)\"\])\s*$/mi.exec($("#filter").val())
    if(nn!=null) {
        var rnn = nn[0].replace(/\[[0-9]+\]/, "[0]");
        $("#filter").val($("#filter").val().replace(nn[0], rnn + ":" + nn[7] + "," + nn[3] + ",[]"))
    }

    var nn=/^\s*([^\:\r\n]*)(\[\"([^\r\n\:]+)\"\])\s*$/mi.exec($("#filter").val())
    if(nn!=null) {
        var rnn = nn[0].replace(/\[[0-9]+\]/, "[0]");
        $("#filter").val($("#filter").val().replace(nn[0], rnn + ":" + nn[3]))
    }
})

function clear(){
    $("#data4").val("");
    $("#data5").val("");
}

